package org.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.demo.entity.*;
import org.demo.mapper.NoticeMapper;
import org.demo.mapper.UsersMapper;
import org.demo.service.INoticeService;
import org.demo.service.IOrderIdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private IOrderIdService orderIdService;


    @Override
    public void add(Notice notice) {

        String id = orderIdService.getId("NOTI");
        notice.setId(id);
        Users adm = usersMapper.selectById(notice.getAdminId());//发件人
        Users use = usersMapper.selectById(notice.getUserId());//收件人

        notice.setContent(adm.getNickname()+","+notice.getContent());
        notice.setShowredio("0");
        notice.setCreateTime(LocalDate.now());
        notice.setAdminName(adm.getNickname());
        notice.setUserName(use.getNickname());

        save(notice);
    }

    @Override
    public void addadmin(Notice notice) {
        String id = orderIdService.getId("NOTI");
        notice.setId(id);
        notice.setContent(notice.getContent());
        notice.setShowredio("3");
        notice.setCreateTime(LocalDate.now());
        notice.setAdminName("管理员");
        save(notice);
    }


    @Override
    public PageBean<Notice> Pagelist(Integer pageNum, Integer pageSize, Notice notice) {
        // 创建 pagebean 对象
        PageBean<Notice> pb = new PageBean<>();
        // 开启分页查询
        Page<Notice> page = PageHelper.startPage(pageNum, pageSize);


        String roleId = notice.getId();
        if (roleId!=null){
            Notice not = new Notice();
            not.setShowredio(notice.getShowredio());
            if (roleId.equals("ROLE_customer")){
                not.setRolesCustomer("1");
            }else if (roleId.equals("ROLE_intermediary")){
                not.setRolesIntermediary("1");
            }else if (roleId.equals("ROLE_landlord")){
                not.setRolesLandlord("1");
            }
            List<Notice> notice2=noticeMapper.selectAllNotice(not);
            List<Notice> notices=noticeMapper.selectAllNotice(notice);
            List<Notice> all=new ArrayList<>();
            all.addAll(notice2);
            all.addAll(notices);
            pb.setTotal(page.getTotal());
            pb.setItems(all);
            return pb;
        }

        List<Notice> notices=noticeMapper.selectAllNotice(notice);


        pb.setTotal(page.getTotal());
        pb.setItems(notices);
        return pb;
    }
    @Override
    public List<Notice> list(Notice notice) {
        String roleId = notice.getId();
        if (roleId!=null){
            Notice not = new Notice();
            not.setShowredio(notice.getShowredio());
            if (roleId.equals("ROLE_customer")){
                not.setRolesCustomer("1");
            }else if (roleId.equals("ROLE_intermediary")){
                not.setRolesIntermediary("1");
            }else if (roleId.equals("ROLE_landlord")){
                not.setRolesLandlord("1");
            }
            List<Notice> notice2=noticeMapper.selectAllNotice(not);
            List<Notice> notices=noticeMapper.selectAllNotice(notice);
            List<Notice> all=new ArrayList<>();
            all.addAll(notice2);
            all.addAll(notices);
            return all;
        }
        List<Notice> notices=noticeMapper.selectAllNotice(notice);
        return notices;
    }

    @Override
    public void update(Notice notice) {
        Notice notice1 = noticeMapper.selectById(notice.getId());
        if (notice1.getAdminName().equals("管理员")){
            notice1.setShowredio("3");
            noticeMapper.updateById(notice1);
        }else {
            notice1.setShowredio("1");
            noticeMapper.updateById(notice1);
        }
    }

    @Override
    public void delete(String id) {
        noticeMapper.deleteById(id);
    }
    @Override
    public void addNotice(String adminId, String userId, String houseId, String content, String rolesCustomer, String rolesIntermediary, String rolesLandlord){
        Notice notice = new Notice();
        String id = orderIdService.getId("NOTI");
        notice.setId(id);
        notice.setUserId(userId);
        notice.setAdminId(adminId);
        Users adm = usersMapper.selectById(notice.getAdminId());//发件人
        Users use = usersMapper.selectById(notice.getUserId());//收件人
        notice.setAdminName(adm.getNickname());
        notice.setUserName(use.getNickname());
        notice.setHouseId(houseId);
        notice.setRolesCustomer(rolesCustomer);
        notice.setRolesLandlord(rolesLandlord);
        notice.setRolesIntermediary(rolesIntermediary);
        notice.setContent(use.getNickname()+","+content);
        notice.setShowredio("0");
        notice.setCreateTime(LocalDate.now());
        save(notice);
    }


}
